package com.tarena.lbs.coupon.dao.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.coupon.dao.mapper.UserCouponsMapper;
import com.tarena.lbs.coupon.dao.repository.UserCouponsRepository;
import com.tarena.lbs.coupon.pojo.po.UserCouponsPO;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class UserCouponsRepositoryImpl implements UserCouponsRepository {

    @Autowired
    private UserCouponsMapper userCouponsMapper;

    @Override
    public PageInfo<UserCouponsPO> pageList(UserCouponQuery query) {
        log.info("=== UserCouponsRepositoryImpl.pageList 开始 ===");
        log.info("分页查询用户优惠券列表，入参：{}", query);
        
        // 启动分页
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        
        // 执行查询
        List<UserCouponsPO> userCouponsList = userCouponsMapper.getUserCouponList(query);
        
        // 封装分页信息
        PageInfo<UserCouponsPO> pageInfo = new PageInfo<>(userCouponsList);
        
        log.info("分页查询用户优惠券列表成功，共{}条数据", pageInfo.getTotal());
        log.info("=== UserCouponsRepositoryImpl.pageList 结束 ===");
        return pageInfo;
    }

    @Override
    public void save(UserCouponsPO userCouponsPO) {
        log.info("=== UserCouponsRepositoryImpl.save 开始 ===");
        log.info("保存用户优惠券记录，入参：{}", userCouponsPO);
        
        try {
            userCouponsMapper.insert(userCouponsPO);
            log.info("保存用户优惠券记录成功，ID：{}", userCouponsPO.getId());
            log.info("=== UserCouponsRepositoryImpl.save 结束 ===");
        } catch (Exception e) {
            log.error("保存用户优惠券记录异常：{}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public UserCouponsPO getUserCouponByCode(String couponCode) {
        log.info("=== UserCouponsRepositoryImpl.getUserCouponByCode 开始 ===");
        log.info("通过优惠券编码查询用户领取详情，优惠券编码：{}", couponCode);
        
        try {
            UserCouponsPO result = userCouponsMapper.getUserCouponByCode(couponCode);
            log.info("通过优惠券编码查询用户领取详情成功，结果：{}", result);
            log.info("=== UserCouponsRepositoryImpl.getUserCouponByCode 结束 ===");
            return result;
        } catch (Exception e) {
            log.error("通过优惠券编码查询用户领取详情异常：{}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public void update(UserCouponsPO userCouponsPO) {
        log.info("=== UserCouponsRepositoryImpl.update 开始 ===");
        log.info("更新用户优惠券记录，入参：{}", userCouponsPO);
        
        try {
            userCouponsMapper.updateById(userCouponsPO);
            log.info("更新用户优惠券记录成功，ID：{}", userCouponsPO.getId());
            log.info("=== UserCouponsRepositoryImpl.update 结束 ===");
        } catch (Exception e) {
            log.error("更新用户优惠券记录异常：{}", e.getMessage(), e);
            throw e;
        }
    }
}
